class Solution {
public:
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int n = grid.size(), m = grid[0].size();
        int ans = 0;
        vector<vector<bool>> vis(n, vector<bool>(m, false));

        function<void(int, int, int&)> dfs = [&](int i, int j, int& ret) {
            vis[i][j] = true;
            ret++;
            ans = max(ans, ret);
            for (int a = 0; a < 4; a++)
            {
                int x = dx[a] + i, y = dy[a] + j;
                if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && grid[x][y] == 1)
                    dfs(x, y, ret);
            }
            };

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (!vis[i][j] && grid[i][j] == 1)
                {
                    int ret = 0;
                    dfs(i, j, ret);
                }
            }
        }
        return ans;
    }
};